
const {LinValidator,Rule} = require('../../core/lin-validator')
const { loginType } = require('../lib/enum')
class isInt extends LinValidator{
    constructor(min = 1,max = 99999999){
        super()
        this.id = [
            new Rule('isInt','不是正整数',{min:min,max:max})
        ]
    }
}
class isRegister extends LinValidator{
    constructor(min = 4,max = 16){
        super()
        this.password = [
            new Rule('matches','必须包含大小写字母和数字的组合，不能使用特殊字符，长度在 6-16 之间',/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{6,16}$/)
        ]
        this.password1 = this.password
        this.nickName = [
            new Rule('isLength',`昵称最小为${min}位，最大不超过${max}位`,{min:min,max:max})
        ]
        this.email = [
            new Rule('isEmail',`邮箱不正确`)
        ]
    }
    validatePassword(val){
        let pws1 = val.body.password
        let pws2 = val.body.password1
        if(pws1 !== pws2){
            throw new Error('两次密码不一致！')
        }
    }
}
class isToken extends LinValidator{
    constructor(min = 4, max = 32){
        super()
        this.account = [
            new Rule('isLength','不符合账号规则',{
                min,
                max
            })
        ]
        this.secret = [
            new Rule('isOptional'),
            new Rule('matches','必须包含大小写字母和数字的组合，不能使用特殊字符，长度在 6-16 之间',/^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z0-9]{6,16}$/)
        ]
    }

    validateLoginType(vals){
        if(!vals.body.type){
            throw new Error('type是必传参数')
        }
        if(!loginType.isType(vals.body.type)){
            throw new Error('type不符合规范')
        }

    }
}

class isAccessToken extends LinValidator{
    constructor(min = 1){
        super()
        this.refreshToken = [
            new Rule('isLength','不正确',{
                min
            })
        ]
    }
}
module.exports = {
    isInt,
    isToken,
    isAccessToken,
    isRegister
}